﻿/*
Copyright (C) 2009  Tomasz Chrzanowski

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Com.TomCdc.ChangeDataCapture
{
    internal class ListViewInstallItem:ListViewItem
    {
        /// <summary>
        /// The icon name which represents running installation step.
        /// </summary>
        private const string ICON_INSTALL_STEP_RUNNING = "InstallStepRunning.ico";

        /// <summary>
        /// The icon name which represents succeeded installation step.
        /// </summary>
        private const string ICON_INSTALL_STEP_SUCCESS = "InstallStepSuccess.ico";

        /// <summary>
        /// The icon name which represents failed installation step.
        /// </summary>
        private const string ICON_INSTALL_STEP_FAILURE = "InstallStepFailure.ico";

        /// <summary>
        /// The icon name which represents idle, not run installation step.
        /// </summary>
        private const string ICON_EMPTY = "EmptyIcon.ico";

        private InstallStepStatus _installStatus;
        internal InstallStepStatus InstallStatus
        {
            get
            {
                return _installStatus;
            }

            set
            {
                _installStatus = value;
            }
        }

        private Exception _installationException;
        internal Exception InstallationException
        {
            get
            {
                return _installationException;
            }

            set
            {
                _installationException = value;
            }
        }



        private SourceTable _sourceTable;
        /// <summary>
        /// Gets an <see cref="SourceTable"/> object, which is represented by this 
        /// <see cref="ListViewInstallItem"/> object.
        /// </summary>
        internal SourceTable TrackedTable
        {
            get
            {
                return _sourceTable;
            }
        }

        /// <summary>
        /// Gets a name of the table, which is represented by this
        /// <see cref="ListViewInstallItem"/> object.
        /// </summary>
        internal string TableIdentifier
        {
            get
            {
                return _sourceTable.TableIdentifier;
            }
        }

        /// <summary>
        /// Initializes an instance of <see cref="ListViewInstallItem"/> object.
        /// </summary>
        /// <param name="sourceTable">A source table the object will represent.</param>
        internal ListViewInstallItem(SourceTable sourceTable)
        {
            // validates input argument
            if (sourceTable == null)
            {
                throw new ArgumentNullException("sourceTable");
            }

            // adds new column to list view item
            SubItems.Add("");
            
            // gives a names to subitems so it can be referenced with no confusion
            SubItems[0].Name = "StepName";
            SubItems[1].Name = "StepStatus";

            // sets step name content
            if (sourceTable.IsDropTrackingRequested())
            {
                SubItems["StepName"].Text = " Uninstall tracking: " + sourceTable.TableIdentifier;
            }
            else
            {
                SubItems["StepName"].Text = " Install tracking: " + sourceTable.TableIdentifier;
            }

            // stores SourceTable reference used during installation
            _sourceTable = sourceTable;

            // sets list view row status (icon and status desc)
            _installStatus = InstallStepStatus.NotStarted;
            SetStepUiStatus();
        }


        internal void SetStepUiStatus()
        {
            // by default subitems use the same style like the item
            UseItemStyleForSubItems = true;

            
            // based on current installation status, set icon and status description of the item
            switch (_installStatus)
            {
                case InstallStepStatus.Running:
                    ImageKey = ICON_INSTALL_STEP_RUNNING;
                    SubItems["StepStatus"].Text = "In Progress";
                    break;
                case InstallStepStatus.Failure:
                    ImageKey = ICON_INSTALL_STEP_FAILURE;
                    SubItems["StepStatus"].Text = "Failure";

                    // when failure, set the status info style to blue font and underlined
                    SubItems["StepStatus"].ForeColor = Color.Blue;
                    SubItems["StepStatus"].Font = new Font("Microsoft Sans Serif", (float)8.25, FontStyle.Underline);
                    // notifies of different subitem style which has been definied (in the code above)
                    UseItemStyleForSubItems = false;
                    break;
                case InstallStepStatus.Success:
                    ImageKey = ICON_INSTALL_STEP_SUCCESS;
                    SubItems["StepStatus"].Text = "Success";
                    break;
                case InstallStepStatus.NotStarted:
                    ImageKey = ICON_EMPTY;
                    SubItems["StepStatus"].Text = "In Queue";
                    break;
                case InstallStepStatus.Cancelling:
                    SubItems["StepStatus"].Text = "Cancelling";
                    break;
                case InstallStepStatus.Cancelled:
                    ImageKey = ICON_INSTALL_STEP_FAILURE;
                    SubItems["StepStatus"].Text = "Cancelled";
                    break; 
            }
        }
    }
}
